<?php
use \think\Db;
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件




/*获取http类型*/
function http_type(){
 $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
 return $http_type;
}

/*分类转小写替换符号为-*/
function change_lower_symbol($name){
  return preg_replace('/[^0-9a-zA-Z]+/i','-',strtolower($name));
}

/*分类向上取*/
function class_uplower($id,$data=array()){
	$self=Db::table('lily_classify')->field('cname,route_name,fid,id,is_tag,route_id,alias_name')->where([['status','=',1],['id','=',$id]])->order('seq','asc')->find();
    if(empty($self)){
        return $data;
    }
	$data[]=$self;
	if($self['fid']==0){
		return $data;
	}else{
		return class_uplower($self['fid'],$data);
	}
}

/*分类向下获取到最小分类id*/
function class_down($id = 0,&$arr) {
    $data = Db::table('lily_classify')->where('fid', $id)->field('id,fid')->select();
    if($data){
        foreach ($data as $k => $data_) {
            $d = class_down($data_['id'],$arr);
            if(!$d){
                $arr[]=$data_['id'];
            }
        }
    }else{
        $arr[]=$id;
    }
    return $arr;
}
/*获取所有子级*/
function class_down_all($id = 0,&$arr) {
    $arr[]=$id;
    $data = Db::table('lily_classify')->where('fid', $id)->field('id,fid')->select();
    if($data){
        foreach ($data as $k => $data_) {
            $d = class_down_all($data_['id'],$arr);
            if(!$d){
                $arr[]=$data_['id'];
            }
        }
    }else{
        $arr[]=$id;
    }
    return $arr;
}

 /*
 面包屑
 除了产品之下的页面为 Product Categories 开头
 其余都用 Home 开头
 */
function get_crumbs($id){
  $total=class_uplower($id);
  /*dump($total);exit;*/
  $class_url='';
  $new_total=array();
  for($i=count($total)-1; $i>=0 ; $i--) {

      if($total[$i]['is_tag']!='1'&&$total[$i]['route_id']!=0){
        if(!empty($total[$i]['route_name'])){
            $class_url.=$total[$i]['route_name'].'/';
        }else{
            $class_url.=change_lower_symbol($total[$i]['cname']).'/';
        }
      

        $total[$i]['crumbs_url']=$class_url;
          /*更改 Products 为 Product Categories*/
        if($total[$i]['cname']=='Products'){
          $total[$i]['cname']='Product Categories';
        }
        $new_total[]=$total[$i];
      }
  }
  return $new_total;
}


/*冒泡排序 从小到大*/
function bubbleSort($arr)
{
  $len=count($arr);
  //该层循环控制 需要冒泡的轮数
  for($i=1;$i<$len;$i++)
  { //该层循环用来控制每轮 冒出一个数 需要比较的次数
    for($k=0;$k<$len-$i;$k++)
    {
       if($arr[$k]>$arr[$k+1])
        {
            $tmp=$arr[$k+1];
            $arr[$k+1]=$arr[$k];
            $arr[$k]=$tmp;
        }
    }
  }
  return $arr;
}

function del_gclid2($url,$where){
  $parsed = parse_url($url);
  $lily_ad=Db::table('lily_ad')->field('pname')->select();
  if(isset($parsed['query'])){
    parse_str($parsed['query'], $params);
    unset($params[$where]);
    unset($params['page']);
    if(!empty($params)){
      $string="";
      foreach ($params as $key => $value) {
        $string.="&".$key."=".$value;
      }
    }else{
      $string="";
    }
    $a=explode('?',$url);
    return $a[0]."?".ltrim($string,'&');
  }else{
    return $url;
  }
}

/*链接移除广告参数*/
function del_gclid($url){
  $parsed = parse_url($url);
  $lily_ad=Db::table('lily_ad')->field('pname')->select();
  if(isset($parsed['query'])){
    parse_str($parsed['query'], $params);
    foreach ($lily_ad as $key => $value) {
      unset($params[$value['pname']]);/*移除广告参数*/
    }
    if(!empty($params)){
      $string="";
      foreach ($params as $key => $value) {
        $string.="&".$key."=".$value;
      }
    }else{
      $string="";
    }
    $a=explode('?',$url);
    return $a[0]."?".ltrim($string,'&');
  }else{
    return $url;
  }
}
/*获取url*/
function get_class_uplower_url($id){
    $total=class_uplower($id);
    $class_url='';
    for($i=count($total)-1; $i>=0 ; $i--) {
        if($total[$i]['is_tag']!='1'){
            if(!empty($total[$i]['route_name'])){
                $class_url.=$total[$i]['route_name'].'/';
            }else{
                $class_url.=change_lower_symbol($total[$i]['cname']).'/';
            }
        }
    }
    return $class_url;
}


function file_name(){
  $name=date("YmdHis",time()).rand(1000,9999);
  return $name;
}


function isMobile(){ 
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])){
        return TRUE;
    }
    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset ($_SERVER['HTTP_VIA'])){
        return stristr($_SERVER['HTTP_VIA'], "wap") ? TRUE : FALSE;// 找不到为flase,否则为TRUE
    } 
    // 判断手机发送的客户端标志,兼容性有待提高
    if (isset ($_SERVER['HTTP_USER_AGENT'])) {
        $clientkeywords = array (
            'mobile',
            'nokia',
            'sony',
            'ericsson',
            'mot',
            'samsung',
            'htc',
            'sgh',
            'lg',
            'sharp',
            'sie-',
            'philips',
            'panasonic',
            'alcatel',
            'lenovo',
            'iphone',
            'ipod',
            'blackberry',
            'meizu',
            'android',
            'netfront',
            'symbian',
            'ucweb',
            'windowsce',
            'palm',
            'operamini',
            'operamobi',
            'openwave',
            'nexusone',
            'cldc',
            'midp',
            'wap'
            ); 
        // 从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))){
            return TRUE;
        }
    }
    if (isset ($_SERVER['HTTP_ACCEPT'])){ // 协议法，因为有可能不准确，放到最后判断
        // 如果只支持wml并且不支持html那一定是移动设备
        // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== FALSE) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === FALSE || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))){
            return TRUE;
        }
    }
    return FALSE;
}


/*分类向上取去掉最顶层一类*/
function class_uplower_delete_first($id,$data=array()){
    $self=Db::table('lily_classify')->field('cname,route_name,fid,id,is_tag,route_id')->where([['status','=',1],['id','=',$id]])->order('seq','asc')->find();
    $data[]=$self;
    if($self['fid']==1){
        return $data;
    }else{
        return class_uplower_delete_first($self['fid'],$data);
    }
}